<?php

namespace App\Admin\Actions;

use Encore\Admin\Actions\Action;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class RunAction extends Action
{
    public $name = 'Run';

    protected $selector = '.run-action';

    public function handle(Request $request, Model $model)
    {
        // 在这里定义按钮的行为
        // 例如，执行某个操作
        // $model->runSomeAction();

        // 返回成功信息
        return $this->response()->success('Run action executed.')->refresh();
    }

    public function dialog()
    {
        $this->confirm('Are you sure you want to run this action?');
    }

    public function render()
    {
        // 注册脚本
        \Encore\Admin\Facades\Admin::script($this->script());

        return "<a class='btn btn-sm btn-primary run-action'>Run</a>";
    }

    protected function script()
    {
        return <<<SCRIPT
$('.run-action').on('click', function () {
    // 这里可以添加一些自定义的 JavaScript 行为
    console.log("Cs");
});
SCRIPT;
    }
}
